package com.aidex.system.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.system.domain.SpecialCode;
import com.aidex.system.service.SpecialCodeService;

/**
 * 特货代码Controller
 * @author jmy
 * @email jmyinjg@163.com
 * @date 2021-12-13
 */
@RestController
@RequestMapping("/system/specialCode")
public class SpecialCodeController extends BaseController
{
    @Autowired
    private SpecialCodeService specialCodeService;

    /**
     * 查询特货代码列表
     */
    @PreAuthorize("@ss.hasPermi('system:specialCode:list')")
    @GetMapping("/list")
    public R<PageInfo> list(SpecialCode specialCode, HttpServletRequest request, HttpServletResponse response)
    {
        specialCode.setPage(new PageDomain(request, response));
        return R.data(specialCodeService.findPage(specialCode));
    }

    @GetMapping("all")
    public R<List<String>> listAll() {
        return R.data(specialCodeService.findList(new SpecialCode()).stream()
                .map(SpecialCode::getSpecialCode).collect(Collectors.toList()));
    }

    /**
     * 获取特货代码详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:specialCode:query')")
    @GetMapping(value = "/{id}")
    public R<SpecialCode> detail(@PathVariable("id") String id)
    {
        return R.data(specialCodeService.get(id));
    }

    /**
     * 新增特货代码
     */
    @PreAuthorize("@ss.hasPermi('system:specialCode:add')")
    @Log(title = "特货代码", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  SpecialCode specialCode)
    {
        return R.status(specialCodeService.save(specialCode));
    }

    /**
     * 修改特货代码
     */
    @PreAuthorize("@ss.hasPermi('system:specialCode:edit')")
    @Log(title = "特货代码", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated SpecialCode specialCode)
    {
        return R.status(specialCodeService.save(specialCode));
    }


    /**
     * 删除特货代码
     */
    @PreAuthorize("@ss.hasPermi('system:specialCode:remove')")
    @Log(title = "特货代码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(specialCodeService.deleteSpecialCodeByIds(ids));
    }


    /**
     * 导出特货代码列表
     */
    @PreAuthorize("@ss.hasPermi('system:specialCode:export')")
    @Log(title = "特货代码", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(SpecialCode specialCode)
    {
        List<SpecialCode> list = specialCodeService.findList(specialCode);
        ExcelUtil<SpecialCode> util = new ExcelUtil<>(SpecialCode.class);
        return util.exportExcel(list, "specialCode");
    }

}
